<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="批量添加创意标题" :visible.sync="dialogVisible" @close="resetForm('batchAddCreatTitleRef')" width="600px">
        <el-form label-position="top" label-width="auto" size="medium" :hide-required-asterisk='true' :model="batchAddCreatTitleForm" :rules="batchAddCreatTitleRules" ref="batchAddCreatTitleRef" v-loading="loading">
            <el-form-item label="" prop="industry" style="margin-bottom:0">
                <div class="font_13 color_03">最多20个标题，每个不超过50个字，可用按Enter换行隔开： {{hasCheck.length}}/{{10-maxLength}}</div>
                <el-input type="textarea" ref="textArea" :rows="8" placeholder="请输入标签内容，用换行隔开" @input="getText" v-model="batchAddCreatTitleForm.industry">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('batchAddCreatTitleRef')" class="el-button-width" size="small">取 消</el-button>
            <el-button type="primary" @click="submitForm('batchAddCreatTitleRef')" class="el-button-width" size="small">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    props:['keyWordList'],
    name: "",
    data() {
        return {
            disabled: false,
            dialogVisible: false,
            hasCheck: [],
            maxLength: 0,
            batchAddCreatTitleForm: {
                industry: "",
            },
            batchAddCreatTitleRules: {
                industry: [
                    {
                        required: true,
                        message: "请输入标题",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },

    created() {
        this.$bus.on("batchAddCreatTitle", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.maxLength = this.$parent.createform.creattitle.title.length;
        });
    },
    beforeDestroy() {
        this.$bus.off("batchAddCreatTitle");
    },
    methods: {
        getText() {
            let newsText = [];
            let arr = this.batchAddCreatTitleForm.industry
                .replace(/\r\n/g, "|")
                .replace(/\n/g, "|")
            let text = arr.split("|").filter(Boolean);
            for (let i in text) {
                newsText.push({
                    title: text[i],
                    dynamic_words: [],
                });
            }
            this.hasCheck = newsText;
        },

        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    if (this.hasCheck.length > 10 - this.maxLength) {
                        this.$message.error("标题最多只能添加10个！");
                        return;
                    } else {
                        let oldData = this.$parent.createform.creattitle.title.filter((item)=>{
                            if(item.title){
                                return item
                            }
                        })
                        let newArr = oldData.concat(this.hasCheck);
                        this.$parent.createform.creattitle.title = [...new Set(newArr)];
                        for(let i in this.$parent.createform.creattitle.title){
                            this.$parent.changeWordTitle(this.$parent.createform.creattitle.title[i],i)
                        }
                        this.resetForm(formName);
                    }
                } else {
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
<style scoped>
</style>

